<?php
/**
 *
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/7/12
 * Time: 下午6:02
 * Updatetime:
 * updater：
 */
namespace WAdmin\Controller;

class CarController extends BaseController
{

    /**
     * @desc car status list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carStatusListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_STATUS_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_STATUS_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_STATUS_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_STATUS_4')];

        return showList($list,new \stdClass());
    }


    /**
     * @desc 车辆型号列表
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carTypeListInterface()
    {
        if ($_POST['l']=='en-us'){
            $car_type_list = M('car_type')->field('id,en_name text')->select();
        }else{
            $car_type_list = M('car_type')->field('id,cn_name text')->select();
        }
        return showList($car_type_list,new \stdClass());
    }


    /**
     * @desc logo list
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    public function carLogoListInterface()
    {
        $list = M('car_logo')->select();
        return showList($list,new \stdClass());
    }

    /**
     * @desc color list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carColorListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_COLOR_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_COLOR_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_COLOR_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_COLOR_4')];
        $list[] = ['id'=>'5','text'=>L('CAR_COLOR_5')];
        $list[] = ['id'=>'6','text'=>L('CAR_COLOR_6')];
        $list[] = ['id'=>'7','text'=>L('CAR_COLOR_7')];
        $list[] = ['id'=>'8','text'=>L('CAR_COLOR_8')];
        $list[] = ['id'=>'9','text'=>L('CAR_COLOR_9')];
        $list[] = ['id'=>'10','text'=>L('CAR_COLOR_10')];
        return showList($list,new \stdClass());
    }

    /**
     * @desc 登录列表
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carPersonLoginListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_PERSONLOGIN_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_PERSONLOGIN_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 汽车货柜类型
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carContainerTypeListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_CONTAINER_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_CONTAINER_TYPE_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 后门类型
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carBackDoorTypeListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_BACK_DOOR_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_BACK_DOOR_TYPE_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 汽车列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');


        if (!empty($param['car_no'])){
            $map['car_no'] = array('like','%'.$param['car_no'].'%');
        }

        if (!empty($param['equipment_no'])){
            $map['equipment_no'] = array('like','%'.$param['equipment_no'].'%');
        }

        if (!empty($param['sim_no'])){
            $map['sim_no'] = array('like','%'.$param['sim_no'].'%');
        }

        $map['uid'] = $this->userInfo->id;

        if ($param['type']==2){
            list($list,$page) = _listPage($model,null,1,'create_time desc','id,car_no,headsmall,equipment_no,sim_no,last_call_time,uid,status',1000);
        }else{
            list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,car_no,headsmall,equipment_no,sim_no,last_call_time,uid,status',$listRows);
        }

        return showList($list,$page);
    }

    /**
     * @desc 删除汽车
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('ids');

        unset($check);

        $ids = $param['ids'];

        $model = M('car');

        foreach (explode(',',$ids) as $k=>$v){

            $carInfo = $model->where(['id'=>$v,'uid'=>$this->userInfo->id])->find();
            if (empty($carInfo)) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);
        }


        $map['id'] = array('in',$param['ids']);

        $operation = $model->where($map)->delete();

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 车辆状态：1 正常，2 停用，3 运营，4 停运
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carStatusText($k)
    {
        $list[1] = L('CAR_STATUS_1');
        $list[2] = L('CAR_STATUS_2');
        $list[3] = L('CAR_STATUS_3');
        $list[4] = L('CAR_STATUS_4');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carStatusText2($k)
    {
        $list[1] = "正常";
        $list[2] = "停用";
        $list[3] ="运营";
        $list[4] = "停运";
        return empty($k)?$list[1]:$list[$k];
    }


    /**
     * @desc 车辆状态列表
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    private function carStatusList()
    {
        $list[] = ['id'=>'1','text'=>"正常"];
        $list[] = ['id'=>'2','text'=>"停用"];
        $list[] = ['id'=>'3','text'=>"运营"];
        $list[] = ['id'=>'4','text'=>"停运"];
        return $list;
    }

    /**
     * @desc logo list
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    private function carLogoList()
    {
        $list = M('car_logo')->select();
        return $list;
    }

    /**
     * @desc color text
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carColorText($k)
    {
        $list[1] = L('CAR_COLOR_1');
        $list[2] = L('CAR_COLOR_2');
        $list[3] = L('CAR_COLOR_3');
        $list[4] = L('CAR_COLOR_4');
        $list[5] = L('CAR_COLOR_5');
        $list[6] = L('CAR_COLOR_6');
        $list[7] = L('CAR_COLOR_7');
        $list[8] = L('CAR_COLOR_8');
        $list[9] = L('CAR_COLOR_9');
        $list[10] = L('CAR_COLOR_10');
        return empty($k)?$list[10]:$list[$k];
    }

    private function carColorText2($k)
    {
        $list[1] = "白色";
        $list[2] = "橘黄";
        $list[3] = "黑色";
        $list[4] = "红色";
        $list[5] ="黄色";
        $list[6] = "灰色";
        $list[7] = "蓝色";
        $list[8] = "绿色";
        $list[9] = "银色";
        $list[10] = "其他";
        return empty($k)?$list[10]:$list[$k];
    }

    /**
     * @desc color list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    private function carColorList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_COLOR_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_COLOR_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_COLOR_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_COLOR_4')];
        $list[] = ['id'=>'5','text'=>L('CAR_COLOR_5')];
        $list[] = ['id'=>'6','text'=>L('CAR_COLOR_6')];
        $list[] = ['id'=>'7','text'=>L('CAR_COLOR_7')];
        $list[] = ['id'=>'8','text'=>L('CAR_COLOR_8')];
        $list[] = ['id'=>'9','text'=>L('CAR_COLOR_9')];
        $list[] = ['id'=>'10','text'=>L('CAR_COLOR_10')];
        return $list;
    }

    private function carColorList2()
    {
        $list[] = ['id'=>'1','text'=>"白色"];
        $list[] = ['id'=>'2','text'=>"橘黄"];
        $list[] = ['id'=>'3','text'=>"黑色"];
        $list[] = ['id'=>'4','text'=>"红色"];
        $list[] = ['id'=>'5','text'=>"黄色"];
        $list[] = ['id'=>'6','text'=>"灰色"];
        $list[] = ['id'=>'7','text'=>"蓝色"];
        $list[] = ['id'=>'8','text'=>"绿色"];
        $list[] = ['id'=>'9','text'=>"银色"];
        $list[] = ['id'=>'10','text'=>"其他"];
        return $list;
    }


    /**
     * @desc cat personal login status 1 allow ,2 deny
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carPersonLoginText($k)
    {
        $list[1] = L('CAR_PERSONLOGIN_1');
        $list[2] = L('CAR_PERSONLOGIN_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carPersonLoginText2($k)
    {
        $list[1] = "允许";
        $list[2] = "拒绝";
        return empty($k)?$list[1]:$list[$k];
    }

    private function carPersonLoginList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_PERSONLOGIN_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_PERSONLOGIN_2')];
        return $list;
    }

    private function carPersonLoginList2()
    {
        $list[] = ['id'=>'1','text'=>"允许"];
        $list[] = ['id'=>'2','text'=>"拒绝"];
        return $list;
    }


    private function carContainerTypeText($k)
    {
        $list[1] = L('CAR_CONTAINER_TYPE_1');
        $list[2] = L('CAR_CONTAINER_TYPE_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carContainerTypeText2($k)
    {
        $list[1] = "干箱";
        $list[2] = "温控";
        return empty($k)?$list[1]:$list[$k];
    }

    private function carContainerTypeList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_CONTAINER_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_CONTAINER_TYPE_2')];
        return $list;
    }

    private function carContainerTypeList2()
    {
        $list[] = ['id'=>'1','text'=>"干箱"];
        $list[] = ['id'=>'2','text'=>"温控"];
        return $list;
    }


    private function carBackDoorTypeText($k)
    {
        $list[1] = L('CAR_BACK_DOOR_TYPE_1');
        $list[2] = L('CAR_BACK_DOOR_TYPE_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carBackDoorTypeText2($k)
    {
        $list[1] = "卷门";
        $list[2] = "扇门";
        return empty($k)?$list[1]:$list[$k];
    }

    private function carBackDoorTypeList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_BACK_DOOR_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_BACK_DOOR_TYPE_2')];
        return $list;
    }

    private function carBackDoorTypeList2()
    {
        $list[] = ['id'=>'1','text'=>"卷门"];
        $list[] = ['id'=>'2','text'=>"扇门"];
        return $list;
    }

    //汽车详情公用list
    public function carPublic($param){
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('car');

        $info = $model->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if ($_POST['l']=='en-us'){//英文版

            $info['status_text'] = $this->carStatusText($info['status']);

            $info['status_list'] = $this->carStatusList();

            $car_logo_url = M('car_logo')->where(['id'=>$info['logo']])->getField('cover_img');
            $info['logo_url'] = !$car_logo_url?'':$car_logo_url;

            $info['logo_list'] = $this->carLogoList();

            $info['color_status'] = $this->carColorText($info['color']);

            $info['color_list'] = $this->carColorList();
            $info['person_login_text'] = $this->carPersonLoginText($info['person_login']);
            $info['person_login_list'] = $this->carPersonLoginList();

            $info['container_type_text'] = $this->carContainerTypeText($info['container_type']);
            $info['container_type_list'] = $this->carContainerTypeList();


            $info['back_door_type_text'] = $this->carBackDoorTypeText($info['back_door_type']);
            $info['back_door_type_list'] = $this->carBackDoorTypeList();

            $car_type = M('car_type')->where(['id'=>$info['car_type']])->getField('en_name');
            $info['car_type_text']=empty($car_type)?'':$car_type;
            $info['car_type_list'] = M('car_type')->field('id,en_name text')->select();
        }else{//中文版

            $info['status_text'] = $this->carStatusText2($info['status']);

            $info['status_list'] = $this->carStatusList();

            $car_logo_url = M('car_logo')->where(['id'=>$info['logo']])->getField('cover_img');
            $info['logo_url'] = !$car_logo_url?'':$car_logo_url;

            $info['logo_list'] = $this->carLogoList();

            $info['color_status'] = $this->carColorText2($info['color']);

            $info['color_list'] = $this->carColorList2();
            $info['person_login_text'] = $this->carPersonLoginText2($info['person_login']);
            $info['person_login_list'] = $this->carPersonLoginList2();

            $info['container_type_text'] = $this->carContainerTypeText2($info['container_type']);
            $info['container_type_list'] = $this->carContainerTypeList2();


            $info['back_door_type_text'] = $this->carBackDoorTypeText2($info['back_door_type']);
            $info['back_door_type_list'] = $this->carBackDoorTypeList2();

            $car_type = M('car_type')->where(['id'=>$info['car_type']])->getField('cn_name');
            $info['car_type_text']=empty($car_type)?'':$car_type;
            $info['car_type_list'] = M('car_type')->field('id,cn_name text')->select();
        }


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }



    /**
     * @desc 汽车详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');

        $info = $model->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

//        $info['status_text'] = $this->carStatusText($info['status']);
//
//        $info['status_list'] = $this->carStatusList();
//
//        $car_logo_url = M('car_logo')->where(['id'=>$info['logo']])->getField('cover_img');
//        $info['logo_url'] = !$car_logo_url?'':$car_logo_url;
//
//        $info['logo_list'] = $this->carLogoList();
//
//        $info['color_status'] = $this->carColorText($info['color']);
//
//        $info['color_list'] = $this->carColorList();
//        $info['person_login_text'] = $this->carPersonLoginText($info['person_login']);
//        $info['person_login_list'] = $this->carPersonLoginList();
//
//        $info['container_type_text'] = $this->carContainerTypeText($info['container_type']);
//        $info['container_type_list'] = $this->carContainerTypeList();
//
//
//        $info['back_door_type_text'] = $this->carBackDoorTypeText($info['back_door_type']);
//        $info['back_door_type_list'] = $this->carBackDoorTypeList();

        //详情内容


//        if ($_POST['l']=='en-us'){
//            $info['car_type_text'] = M('car_type')->where(['id'=>$info['car_type']])->getField('en_name');
//            $info['car_type_list'] = M('car_type')->field('id,en_name text')->select();
//        }else{
//            $info['car_type_text'] = M('car_type')->where(['id'=>$info['car_type']])->getField('cn_name');
//            $info['car_type_list'] = M('car_type')->field('id,cn_name text')->select();
//        }

        //设备

        $equipment = M('car_equipment')->where(['car_id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        $equipmentAllArr = M('user_equipment')->where(['uid'=>$this->userInfo->id])->select();

        $equipmentArr = [];

        if (!empty($equipmentAllArr)){
            foreach ($equipmentAllArr as $k=>$v){

                $equipment_info = M('equipment')->field('id,en_name text,cn_name,type')->find($v['equipment_id']);

                $equipmentArr[$equipment_info['cn_name']] = $equipment_info;


                if ($equipment_info['cn_name'] == '湿度报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['humidity'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['humidity'])?$equipment['humidity']:'';
                }

                if ($equipment_info['cn_name'] == '温度报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['temperature'])?$equipment['temperature']:'';
                }


                if ($equipment_info['cn_name'] == '1区温度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['one_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['one_zone_temperature'])?$equipment['one_zone_temperature']:'';
                }


                if ($equipment_info['cn_name'] == '1区湿度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['one_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['one_zone_temperature'])?$equipment['one_zone_temperature']:'';
                }



                if ($equipment_info['cn_name'] == '2区温度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['two_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['two_zone_temperature'])?$equipment['two_zone_temperature']:'';
                }


                if ($equipment_info['cn_name'] == '2区湿度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['two_zone_humidity'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['two_zone_humidity'])?$equipment['two_zone_humidity']:'';
                }


                if ($equipment_info['cn_name'] == '3区温度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['three_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['three_zone_temperature'])?$equipment['three_zone_temperature']:'';
                }


                if ($equipment_info['cn_name'] == '3区湿度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['three_zone_humidity'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['three_zone_humidity'])?$equipment['three_zone_humidity']:'';
                }


                if ($equipment_info['cn_name'] == '4区温度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['four_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['four_zone_temperature'])?$equipment['four_zone_temperature']:'';
                }


                if ($equipment_info['cn_name'] == '4区湿度'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['four_zone_humidity'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['four_zone_humidity'])?$equipment['four_zone_humidity']:'';
                }


                if ($equipment_info['cn_name'] == '掉线报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['offline'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = '';
                }

                if ($equipment_info['cn_name'] == '制冷机开报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['refrigerating_machine'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['refrigerating_machine'])?$equipment['refrigerating_machine']:'';
                }

                if ($equipment_info['cn_name'] == '制冷机关报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['refrigeration_alarm'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['refrigeration_alarm'])?$equipment['refrigeration_alarm']:'';
                }


                if ($equipment_info['cn_name'] == '出区域报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['outside_area'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['outside_area'])?$equipment['outside_area']:'';
                }


                if ($equipment_info['cn_name'] == '入区域报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['inside_area'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['inside_area'])?$equipment['inside_area']:'';
                }


                if ($equipment_info['cn_name'] == '超速报警'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['one_zone_temperature'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['one_zone_temperature'])?$equipment['one_zone_temperature']:'';
                }

                if ($equipment_info['cn_name'] == '低电压'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['low_voltage'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['low_voltage'])?$equipment['low_voltage']:'';
                }

                if ($equipment_info['cn_name'] == '高电压'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['high_voltage'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['high_voltage'])?$equipment['high_voltage']:'';
                }

                if ($equipment_info['cn_name'] == '油量'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['oil_sensor'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = !empty($equipment['oil_sensor'])?$equipment['oil_sensor']:'';
                }


                if ($equipment_info['cn_name'] == '门开关'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['gate_magnetic'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = '';
                }


                if ($equipment_info['cn_name'] == '点火'){

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = !empty($equipment['ignition_line'])? 1:2;

                    $equipmentArr[$equipment_info['cn_name']]['value'] = '';
                }

                unset($equipmentArr[$equipment_info['cn_name']]['cn_name']);
            }
        }

        $info['equipment_list'] = empty($equipmentAllArr)?[]:$equipmentArr;

        $info['car_equipment_id'] = $equipment['id'];

        //浮动框消息设置
        $car_show = M('car_show')->where(['car_id'=>$info['id']])->field("id car_show_id,equipment_title s_equipment_title,gps_time s_gps_time,speed s_speed,status s_status,location s_location,temperature s_temperature,humidity s_humidity,entrance_guard s_entrance_guard,refrigerator s_refrigerator,oil s_oil,mileage_now s_mileage_now,mileage_all s_mileage_all")->find();
        $info['car_float_show'] = $car_show;

        //分组信息

        $info['g_title'] = M('car_group')->where(['id'=>$info['gid']])->getField('title');
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 汽车操作
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carOperation($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('car');
        $model_show = M('car_show');
        $model_car_equipment_select = M('car_equipment_select');
//        $model_car_equipment = M('car_equipment');



        if (!$param['id']){//添加汽车

            $insert_data['uid'] = $this->userInfo->id;

            //基本信息

            if (!empty($param['car_no'])) $insert_data['car_no'] = $param['car_no'];
            if (!empty($param['sim_no'])) $insert_data['sim_no'] = $param['sim_no'];
            if (!empty($param['equipment_no'])) $insert_data['equipment_no'] = $param['equipment_no'];
            if (!empty($param['car_model'])) $insert_data['car_model'] = $param['car_model'];
            if (!empty($param['factor'])) $insert_data['factor'] = $param['factor'];
            if (!empty($param['status'])) $insert_data['status'] = $param['status'];
            if (!empty($param['logo'])) $insert_data['logo'] = $param['logo'];
            if (!empty($param['color'])) $insert_data['color'] = $param['color'];
            if (!empty($param['time_zone'])) $insert_data['time_zone'] = $param['time_zone'];
            if (!empty($param['personal_login'])) $insert_data['personal_login'] = $param['personal_login'];
            if (!empty($param['callback_sec'])) $insert_data['callback_sec'] = $param['callback_sec'];
            if (!empty($param['cj_pwd'])) $insert_data['cj_pwd'] = $param['cj_pwd'];
            if (!empty($param['oil_volume'])) $insert_data['oil_volume'] = $param['oil_volume'];
            if (!empty($param['refrigeration_no'])) $insert_data['refrigeration_no'] = $param['refrigeration_no'];
            if (!empty($param['refrigeration_serial_no'])) $insert_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
            if (!empty($param['container_no'])) $insert_data['container_no'] = $param['container_no'];
            if (!empty($param['container_via_no'])) $insert_data['container_via_no'] = $param['container_via_no'];
            if (!empty($param['container_type'])) $insert_data['container_type'] = $param['container_type'];
            if (!empty($param['back_door_type'])) $insert_data['back_door_type'] = $param['back_door_type'];
            if (!empty($param['install_time'])) $insert_data['install_time'] = $param['install_time'];
            if (!empty($param['admin_info'])) $insert_data['admin_info'] = $param['admin_info'];
            if (!empty($param['remarks'])) $insert_data['remarks'] = $param['remarks'];
            if (!empty($param['admin_info'])) $insert_data['admin_info'] = $param['admin_info'];
            if (!empty($param['factory_no'])) $insert_data['factory_no'] = $param['factory_no'];
            if (!empty($param['car_type'])) $insert_data['car_type'] = $param['car_type'];
            if (!empty($param['operation_license_no'])) $insert_data['operation_license_no'] = $param['operation_license_no'];
            if (!empty($param['engine_no'])) $insert_data['engine_no'] = $param['engine_no'];
            if (!empty($param['chassis_no'])) $insert_data['chassis_no'] = $param['chassis_no'];
            if (!empty($param['length'])) $insert_data['length'] = $param['length'];
            if (!empty($param['volume'])) $insert_data['volume'] = $param['volume'];
            if (!empty($param['route_site'])) $insert_data['route_site'] = $param['route_site'];
            if (!empty($param['frame_no'])) $insert_data['frame_no'] = $param['frame_no'];
            if (!empty($param['headsmall'])) $insert_data['headsmall'] = $param['headsmall'];
            if (!empty($param['equipment_ids'])) $insert_data['equipment_ids'] = $param['equipment_ids'];
            if (!empty($param['gid'])) $insert_data['gid'] = $param['gid'];
            if (!empty($param['th_head_number_1'])) $insert_data['th_head_number_1'] = $param['th_head_number_1'];
            if (!empty($param['th_head_number_2'])) $insert_data['th_head_number_2'] = $param['th_head_number_2'];
            if (!empty($param['th_head_number_3'])) $insert_data['th_head_number_3'] = $param['th_head_number_3'];
            if (!empty($param['th_head_number_4'])) $insert_data['th_head_number_4'] = $param['th_head_number_4'];
            if (!empty($param['company_id'])) $insert_data['company_id'] = $param['company_id'];


            $operation = $model->add($insert_data);

            if ($operation) {

                $insert_data_s = [
                    'car_id'                =>  $operation,
                    'uid'                   =>  $this->userInfo->id,
                    'equipment_title'       =>  empty($param['s_equipment_title'])?0:$param['s_equipment_title'],
                    'gps_time'              =>  empty($param['s_gps_time'])?0:$param['s_gps_time'],
                    'speed'                 =>  empty($param['s_speed'])?0:$param['s_speed'],
                    'status'                =>  empty($param['s_status'])?0:$param['s_status'],
                    'location'              =>  empty($param['s_location'])?0:$param['s_location'],
                    'temperature'           =>  empty($param['s_temperature'])?0:$param['s_temperature'],
                    'humidity'              =>  empty($param['s_humidity'])?0:$param['s_humidity'],
                    'entrance_guard'        =>  empty($param['s_entrance_guard'])?0:$param['s_entrance_guard'],
                    'refrigerator'          =>  empty($param['s_refrigerator'])?0:$param['s_refrigerator'],
                    'oil'                   =>  empty($param['s_oil'])?0:$param['s_oil'],
                    'mileage_now'           =>  empty($param['s_mileage_now'])?0:$param['s_mileage_now'],
                    'mileage_all'           =>  empty($param['s_mileage_all'])?0:$param['s_mileage_all']
                ];

                $operation_s = $model_show->add($insert_data_s);

                if (!empty($param['select_ids'])){
                    $ids=explode(',',$param['select_ids']);
                   foreach ($ids as $val){
                        $select_add=[
                            'uid'=>$this->userInfo->id,
                            'car_id'=>$operation,
                            'equipment_id'=>$val,
                            'create_time'=>time(),
                            'type'=>0
                    ];
                       $model_car_equipment_select->add($select_add);
                    }

                }

//                $insert_data_a = [
//                    'car_id'    =>  $operation,
//                    'uid'       =>  $this->userInfo->id,
//                ];
//                $operation_a = $model_car_equipment_select->add($insert_data_a);

//                if (!empty($param['温度报警'])) $insert_data_equipment['temperature'] = $param['温度报警'];
//                if (!empty($param['湿度报警'])) $insert_data_equipment['humidity'] = $param['湿度报警'];
//                if (!empty($param['一区温度'])) $insert_data_equipment['one_zone_temperature'] = $param['一区温度'];
//                if (!empty($param['一区湿度'])) $insert_data_equipment['one_zone_humidity'] = $param['一区温度'];
//                if (!empty($param['二区温度'])) $insert_data_equipment['two_zone_temperature'] = $param['二区温度'];
//                if (!empty($param['二区湿度'])) $insert_data_equipment['two_zone_humidity'] = $param['二区湿度'];
//                if (!empty($param['三区温度'])) $insert_data_equipment['three_zone_temperature'] = $param['三区温度'];
//                if (!empty($param['三区湿度'])) $insert_data_equipment['three_zone_humidity'] = $param['三区湿度'];
//                if (!empty($param['四区温度'])) $insert_data_equipment['four_zone_temperature'] = $param['四区温度'];
//                if (!empty($param['四区湿度'])) $insert_data_equipment['four_zone_humidity'] = $param['四区湿度'];
//                if (!empty($param['掉线报警'])) $insert_data_equipment['offline'] = $param['掉线报警'];
//                if (!empty($param['制冷机开报警'])) $insert_data_equipment['refrigerating_machine'] = $param['制冷机开报警'];
//                if (!empty($param['制冷机关报警'])) $insert_data_equipment['refrigeration_alarm'] = $param['制冷机关报警'];
//                if (!empty($param['出区域报警'])) $insert_data_equipment['outside_area'] = $param['出区域报警'];
//                if (!empty($param['入区域报警'])) $insert_data_equipment['inside_area'] = $param['入区域报警'];
//                if (!empty($param['超速报警'])) $insert_data_equipment['speed'] = $param['超速报警'];
//                if (!empty($param['低电压报警'])) $insert_data_equipment['low_voltage'] = $param['低电压报警'];
//                if (!empty($param['高电压报警'])) $insert_data_equipment['high_voltage'] = $param['高电压报警'];
//                if (!empty($param['油量报警'])) $insert_data_equipment['oil_sensor'] = $param['油量报警'];
//                if (!empty($param['门开关'])) $insert_data_equipment['gate_magnetic'] = $param['门开关'];
//                if (!empty($param['点火'])) $insert_data_equipment['ignition'] = $param['点火'];
//
//                $insert_data_equipment['uid'] = $this->userInfo->id;
//                $insert_data_equipment['car_id'] = $operation;
//
//                $model_car_equipment->add($insert_data_equipment);

                if ($operation_s) {
                    return showErrorMsg(L("_OPERATION_SUCCESS_"));
                }else{
                    $model->delete($operation);
//                    $model_show->delete($operation_s);
                    return showErrorMsg(L("_OPERATION_FAIL_"),10002);
                }
            }
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }else{//修改
            $save_data_id['id'] = $param['id'];

            if (!empty($param['car_no'])) $save_data['car_no'] = $param['car_no'];
            if (!empty($param['sim_no'])) $save_data['sim_no'] = $param['sim_no'];
            if (!empty($param['equipment_no'])) $save_data['equipment_no'] = $param['equipment_no'];
            if (!empty($param['car_model'])) $save_data['car_model'] = $param['car_model'];
            if (!empty($param['factor'])) $save_data['factor'] = $param['factor'];
            if (!empty($param['status'])) $save_data['status'] = $param['status'];
            if (!empty($param['logo'])) $save_data['logo'] = $param['logo'];
            if (!empty($param['color'])) $save_data['color'] = $param['color'];
            if (!empty($param['time_zone'])) $save_data['time_zone'] = $param['time_zone'];
            if (!empty($param['personal_login'])) $save_data['personal_login'] = $param['personal_login'];
            if (!empty($param['callback_sec'])) $save_data['callback_sec'] = $param['callback_sec'];
            if (!empty($param['cj_pwd'])) $save_data['cj_pwd'] = $param['cj_pwd'];
            if (!empty($param['oil_volume'])) $save_data['oil_volume'] = $param['oil_volume'];
            if (!empty($param['refrigeration_no'])) $save_data['refrigeration_no'] = $param['refrigeration_no'];
            if (!empty($param['refrigeration_serial_no'])) $save_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
            if (!empty($param['container_no'])) $save_data['container_no'] = $param['container_no'];
            if (!empty($param['container_via_no'])) $save_data['container_via_no'] = $param['container_via_no'];
            if (!empty($param['container_type'])) $save_data['container_type'] = $param['container_type'];
            if (!empty($param['back_door_type'])) $save_data['back_door_type'] = $param['back_door_type'];
            if (!empty($param['install_time'])) $save_data['install_time'] = $param['install_time'];
            if (!empty($param['admin_info'])) $save_data['admin_info'] = $param['admin_info'];
            if (!empty($param['remarks'])) $save_data['remarks'] = $param['remarks'];
//            if (!empty($param['admin_info'])) $save_data['admin_info'] = $param['admin_info'];
            if (!empty($param['factory_no'])) $save_data['factory_no'] = $param['factory_no'];
            if (!empty($param['car_type'])) $save_data['car_type'] = $param['car_type'];
            if (!empty($param['operation_license_no'])) $save_data['operation_license_no'] = $param['operation_license_no'];
            if (!empty($param['engine_no'])) $save_data['engine_no'] = $param['engine_no'];
            if (!empty($param['chassis_no'])) $save_data['chassis_no'] = $param['chassis_no'];
            if (!empty($param['length'])) $save_data['length'] = $param['length'];
            if (!empty($param['volume'])) $save_data['volume'] = $param['volume'];
            if (!empty($param['route_site'])) $save_data['route_site'] = $param['route_site'];
            if (!empty($param['frame_no'])) $save_data['frame_no'] = $param['frame_no'];
            if (!empty($param['headsmall'])) $save_data['headsmall'] = $param['headsmall'];
            if (!empty($param['equipment_ids'])) $save_data['equipment_ids'] = $param['equipment_ids'];
            if (!empty($param['gid'])) $save_data['gid'] = $param['gid'];
            if (!empty($param['th_head_number_1'])) $save_data['th_head_number_1'] = $param['th_head_number_1'];
            if (!empty($param['th_head_number_2'])) $save_data['th_head_number_2'] = $param['th_head_number_2'];
            if (!empty($param['th_head_number_3'])) $save_data['th_head_number_3'] = $param['th_head_number_3'];
            if (!empty($param['th_head_number_4'])) $save_data['th_head_number_4'] = $param['th_head_number_4'];
            if (!empty($param['company_id'])) $save_data['company_id'] = $param['company_id'];

            $save_data['update_time'] = time();

            $operation = $model->where(['id'=>$save_data_id['id']])->save($save_data);

            if ($operation){
                if (!empty($param['car_show_id'])){

                    $save_data_s_id['id'] = $param['car_show_id'];
//                    if (!empty($param['s_equipment_title'])) $save_data_s['equipment_title'] = $param['s_equipment_title'];
//                    if (!empty($param['gps_time'])) $save_data_s['gps_time'] = $param['s_gps_time'];
//                    if (!empty($param['s_speed'])) $save_data_s['speed'] = $param['s_speed'];
//                    if (!empty($param['s_status'])) $save_data_s['status'] = $param['s_status'];
//                    if (!empty($param['s_location'])) $save_data_s['location'] = $param['s_location'];
//                    if (!empty($param['s_temperature'])) $save_data_s['temperature'] = $param['s_temperature'];
//                    if (!empty($param['s_humidity'])) $save_data_s['humidity'] = $param['s_humidity'];
//                    if (!empty($param['s_entrance_guard'])) $save_data_s['entrance_guard'] = $param['s_entrance_guard'];
//                    if (!empty($param['s_oil'])) $save_data_s['oil'] = $param['s_oil'];
//                    if (!empty($param['s_mileage_now'])) $save_data_s['mileage_now'] = $param['s_mileage_now'];
//                    if (!empty($param['s_mileage_all'])) $save_data_s['mileage_all'] = $param['s_mileage_all'];
//                    if (!empty($param['s_refrigerator'])) $save_data_s['refrigerator'] = $param['s_refrigerator'];
                    $save_data_s=[
                        'equipment_title'=>empty($param['s_equipment_title'])?0:$param['s_equipment_title'],
                        'gps_time'=>empty($param['s_gps_time'])?0:$param['s_gps_time'],
                        'speed'=>empty($param['s_speed'])?0:$param['s_speed'],
                        'status'=>empty($param['s_status'])?0:$param['s_status'],
                        'location'=>empty($param['s_location'])?0:$param['s_location'],
                        'temperature'=>empty($param['s_temperature'])?0:$param['s_temperature'],
                        'humidity'=>empty($param['s_humidity'])?0:$param['s_humidity'],
                        'entrance_guard'=>empty($param['s_entrance_guard'])?0:$param['s_entrance_guard'],
                        'oil'=>empty($param['s_oil'])?0:$param['s_oil'],
                        'mileage_now'=>empty($param['s_mileage_now'])?0:$param['s_mileage_now'],
                        'mileage_all'=>empty($param['s_mileage_all'])?0:$param['s_mileage_all'],
                        'refrigerator'=>empty($param['s_refrigerator'])?0:$param['s_refrigerator'],
                    ];
                    $operation_s = $model_show->where(['id'=>$save_data_s_id['id']])->save($save_data_s);

//                    if (!$operation_s) {
//                        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
//                    }
                }


                if (!empty($param['select_ids'])){
                    $ids=explode(',',$param['select_ids']);
                    //删除之前设置
                    M('car_equipment_select')->where(['uid'=>$this->userInfo->id,'car_id'=>$param['id']])->delete();

                    //插入新勾选
                    foreach ($ids as $val){
                        $select_add=[
                            'uid'=>$this->userInfo->id,
                            'car_id'=>$param['id'],
                            'equipment_id'=>$val,
                            'create_time'=>time(),
                            'type'=>0
                        ];
                        $model_car_equipment_select->add($select_add);
                    }

                }

//                if (!empty($param['car_equipment_id'])){
//
//                    if (!empty($param['温度报警'])) $save_data_equipment['temperature'] = $param['温度报警'];
//                    if (!empty($param['湿度报警'])) $save_data_equipment['humidity'] = $param['湿度报警'];
//                    if (!empty($param['一区温度'])) $save_data_equipment['one_zone_temperature'] = $param['一区温度'];
//                    if (!empty($param['一区湿度'])) $save_data_equipment['one_zone_humidity'] = $param['一区温度'];
//                    if (!empty($param['二区温度'])) $save_data_equipment['two_zone_temperature'] = $param['二区温度'];
//                    if (!empty($param['二区湿度'])) $save_data_equipment['two_zone_humidity'] = $param['二区湿度'];
//                    if (!empty($param['三区温度'])) $save_data_equipment['three_zone_temperature'] = $param['三区温度'];
//                    if (!empty($param['三区湿度'])) $save_data_equipment['three_zone_humidity'] = $param['三区湿度'];
//                    if (!empty($param['四区温度'])) $save_data_equipment['four_zone_temperature'] = $param['四区温度'];
//                    if (!empty($param['四区湿度'])) $save_data_equipment['four_zone_humidity'] = $param['四区湿度'];
//                    if (!empty($param['掉线报警'])) $save_data_equipment['offline'] = $param['掉线报警'];
//                    if (!empty($param['制冷机开报警'])) $save_data_equipment['refrigerating_machine'] = $param['制冷机开报警'];
//                    if (!empty($param['制冷机关报警'])) $save_data_equipment['refrigeration_alarm'] = $param['制冷机关报警'];
//                    if (!empty($param['出区域报警'])) $save_data_equipment['outside_area'] = $param['出区域报警'];
//                    if (!empty($param['入区域报警'])) $save_data_equipment['inside_area'] = $param['入区域报警'];
//                    if (!empty($param['超速报警'])) $save_data_equipment['speed'] = $param['超速报警'];
//                    if (!empty($param['低电压报警'])) $save_data_equipment['low_voltage'] = $param['低电压报警'];
//                    if (!empty($param['高电压报警'])) $save_data_equipment['high_voltage'] = $param['高电压报警'];
//                    if (!empty($param['油量报警'])) $save_data_equipment['oil_sensor'] = $param['油量报警'];
//                    if (!empty($param['门开关'])) $save_data_equipment['gate_magnetic'] = $param['门开关'];
//                    if (!empty($param['点火'])) $save_data_equipment['ignition'] = $param['点火'];
//
//                    $save_data_equipment['id'] = $param['car_equipment_id'];
//                    $model_car_equipment->save($save_data_equipment);
//
//                }

                return showErrorMsg(L("_OPERATION_SUCCESS_"));

            }
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }

    }

    /**
     * @desc 汽车分组列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car_group');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'num asc ,update_time desc,create_time desc','id,title,num,remark,company_id',$listRows);

        $model_car = M('car');

        $model_company = M('company');

        if (!empty($list)){
            foreach ($list as $k=>$v){

                $title = $model_company->where(['id'=>$v['company_id']])->getField('title');
                $list[$k]['company_title'] = empty($title)?'':$title;

                $car_list = $model_car->field('id,headsmall,car_no,equipment_no,sim_no,last_call_time')->where(['gid'=>$v['id']])->select();
                $list[$k]['_child'] = empty($car_list)?[]:$car_list;
            }
        }
        return showList($list,$page);
    }

    /**
     * @desc 添加群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title');

        unset($check);


        $insert_data = [
            'create_time' => time(),
            'update_time' => time(),
            'uid'   =>  $this->userInfo->id,
            'title' =>  $param['title'],
            'num'  =>  empty($param['num'])?50:$param['num'],
            'company_id'    =>  empty($param['company_id'])?'':$param['company_id'],
            'remark'    =>  empty($param['remark'])?'':$param['remark'],
        ];

        $model = M('car_group');

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc company列表
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyList()
    {
        $list = M('company')->where(['uid'=>$this->userInfo->id])->select();

        return showList($list,new \stdClass());

    }

    public function carGroupSave($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $save_data['id'] = $param['id'];

        if (!empty($param['title'])) $save_data['title'] = $param['title'];
        if (!empty($param['num'])) $save_data['num'] = $param['num'];
        if (!empty($param['remark'])) $save_data['remark'] = $param['remark'];
        if (!empty($param['company_id'])) $save_data['company_id'] = $param['company_id'];
        if (!empty($param['company_id'])) $save_data['company_id'] = $param['company_id'];


        $save_data['update_time'] = time();


        $model = M('car_group');

        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 删除群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        foreach (explode(',',$param['id']) as $k=>$v){
            $operation = $model->delete($v);

            if($operation){
                M('car')->where(['gid'=>$v])->setField('gid','0');

            }else{
                return showErrorMsg(L("_OPERATION_FAIL_"),10002);
            }
        }

        return showErrorMsg(L("_OPERATION_SUCCESS_"));


    }

    /**
     * @desc 添加车到群里
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAddCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('group_id,car_id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['group_id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $model_car = M('car');

        $is_car_exist = $model_car->where(['uid'=>$this->userInfo->id,'id'=>$param['car_id']])->find();
        if (!$is_car_exist) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        $operation = $model_car->where(['id'=>$param['car_id']])->setField('gid',$param['group_id']);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车设备详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carEquipmentDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        //设备
        $equipmentAllArr = M('equipment')->where(['status'=>1])->select();

        if (!empty($param['id'])){
            $equipment_select=M('car_equipment_select')->where(['car_id'=>$param['id']])->getField("equipment_id",true);
        }
        if (!empty($equipment_select)){
            foreach ($equipmentAllArr as &$val){
                if (in_array($val['id'],$equipment_select)){
                    $val['select']=1;
                }else{
                    $val['select']=0;
                }
            }
        }else{
            foreach ($equipmentAllArr as &$vol){
                $vol['select']=0;
            }
        }


        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$equipmentAllArr);
    }

    /**
     * @desc 汽车浮动消息设置详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');
        $is_car_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $carEquipmentInfo = M('car_show')->where(['car_id'=>$param['id']])->find();

        if (!$carEquipmentInfo) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$carEquipmentInfo);
    }

    /**
     * @desc 汽车浮动消息添加
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id');

        unset($check);


        $model = M('car_show');

        $is_exist = $model->where(['car_id'=>$param['car_id']])->find();

        if ($is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_EXIST_"),100002,2,$is_exist);

        $insert_data = [
            'car_id'                =>  $param['car_id'],
            'uid'                   =>  $this->userInfo->id,
            'equipment_title'                 =>  empty($param['equipment_title'])?0:$param['equipment_title'],
            'gps_time'          =>  empty($param['gps_time'])?0:$param['gps_time'],
            'speed'           =>  empty($param['speed'])?0:$param['speed'],
            'status'     =>  empty($param['status'])?0:$param['status'],
            'location'           =>  empty($param['location'])?0:$param['location'],
            'temperature'      =>  empty($param['temperature'])?0:$param['temperature'],
            'humidity'      =>  empty($param['humidity'])?0:$param['humidity'],
            'entrance_guard'               =>  empty($param['entrance_guard'])?0:$param['entrance_guard'],
            'refrigerator'                =>  empty($param['refrigerator'])?0:$param['refrigerator'],
            'oil'              =>  empty($param['oil'])?0:$param['oil'],
            'mileage_now'                 =>  empty($param['mileage_now'])?0:$param['mileage_now'],
            'mileage_all'           =>  empty($param['mileage_all'])?0:$param['mileage_all']
        ];

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车浮动消息修改
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);


        $model = M('car_show');

        $is_exist = $model->where(['id'=>$param['id']])->find();

        if (!$is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_NOT_EXIT_"),100002,2,$is_exist);

        $save_data['id'] = $param['id'];
        if (!empty($param['equipment_title'])) $save_data['equipment_title'] = $param['equipment_title'];
        if (!empty($param['gps_time'])) $save_data['gps_time'] = $param['gps_time'];
        if (!empty($param['speed'])) $save_data['speed'] = $param['speed'];
        if (!empty($param['status'])) $save_data['status'] = $param['status'];
        if (!empty($param['location'])) $save_data['location'] = $param['location'];
        if (!empty($param['temperature'])) $save_data['temperature'] = $param['temperature'];
        if (!empty($param['humidity'])) $save_data['humidity'] = $param['humidity'];
        if (!empty($param['entrance_guard'])) $save_data['entrance_guard'] = $param['entrance_guard'];
        if (!empty($param['refrigerator'])) $save_data['refrigerator'] = $param['refrigerator'];
        if (!empty($param['oil'])) $save_data['oil'] = $param['oil'];
        if (!empty($param['mileage_now'])) $save_data['mileage_now'] = $param['mileage_now'];
        if (!empty($param['mileage_all'])) $save_data['mileage_all'] = $param['mileage_all'];

        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 实时数据
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function rtp($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');

        if (!empty($param['keyword'])){
            $map['car_no'] = array('like','%'.$param['keyword'].'%');
        }

        $map['uid'] = $this->userInfo->id;
//        $map['status'] = 3;

        list($list,$page) = _listPage($model,$map,$p,null,"id,car_no,headsmall,equipment_no,status",$listRows);

        if (!empty($list)){
            foreach ($list as &$v){

            	$carAttr = M('car_attr')->where(['car_id'=>$v['id']])->order('id desc')->find();
            	$where=[
            	    'car_id'=>$v['id'],
                    'type'=>2,
                    'create_time'=>array('egt',strtotime(date('Y-m-d',$carAttr['create_time'])))
                ];

            	$v['car_attr_id'] = $carAttr['id'];
                $v['waring']=M('message')->where($where)->order("id desc")->select();

            	$v['speed'] = empty($carAttr['speed'])?0:$carAttr['speed'];
            	$v['door'] = empty($carAttr['door'])?'':$carAttr['door'];
            	$v['create_time'] = date('Y-m-d H:i:s',$carAttr['create_time']);
            	$v['voltage'] = empty($carAttr['voltage'])?'':$carAttr['voltage'];
            	$v['cold_switch'] = empty($carAttr['cold_switch'])?'':$carAttr['cold_switch'];
            	$v['humidity'] = empty($carAttr['humidity'])?'':$carAttr['humidity'];
            	$v['temperature_one'] = empty($carAttr['temperature_one'])?'':$carAttr['temperature_one'];
            	$v['temperature_two'] = empty($carAttr['temperature_two'])?'':$carAttr['temperature_two'];
            	$v['temperature_three'] = empty($carAttr['temperature_three'])?'':$carAttr['temperature_three'];
            	$v['temperature_four'] = empty($carAttr['temperature_four'])?'':$carAttr['temperature_four'];
            	$v['oil_percents'] = empty($carAttr['oil'])?'':$carAttr['oil'];
            	$v['humidity_one'] = empty($carAttr['humidity_one'])?'':$carAttr['humidity_one'];
            	$v['humidity_two'] = empty($carAttr['humidity_two'])?'':$carAttr['humidity_two'];
            	$v['humidity_three'] = empty($carAttr['humidity_three'])?'':$carAttr['humidity_three'];
            	$v['humidity_four'] = empty($carAttr['humidity_four'])?'':$carAttr['humidity_four'];
            	$v['today_mileage'] = empty($carAttr['today_mileage'])?'':round($carAttr['today_mileage']/1000,2);
            	$v['all_mileage'] = empty($carAttr['all_mileage'])?'':round($carAttr['all_mileage']/1000,2);
            	$v['oil'] = empty($carAttr['oil'])?'':$carAttr['oil'];
            	$v['lat'] = empty($carAttr['lat'])?'':$carAttr['lat'];
            	$v['lng'] = empty($carAttr['lng'])?'':$carAttr['lng'];

                $v['speed_normal'] = $v['speed']<20 || $v['speed']>110 ? 2:1;
                $v['electric_normal'] = $v['electric_normal']<13 ? 2:1;
                $v['oil_percents_normal'] = $v['oil_percents']<25 ? 2:1;
                $v['temperature_one_normal'] = $v['temperature_one']<20 ? 2:1;
                $v['temperature_two_normal'] = $v['temperature_two']<20 ? 2:1;
                $v['temperature_three_normal'] = $v['temperature_three']<20 ? 2:1;
                $v['temperature_four_normal'] = $v['temperature_four']<20 ? 2:1;

            }
        }

        return showList($list,$page);
    }


	/**
	 * @desc 绑定汽车围栏
	 * @param $param
	 * @return array
	 * @author Egor<http://gitee.com/seosite>
	 */
    public function carBandEnclosure($param)
	{
		$model = M('car_enclosure_bind');

		$is_exist = $model->where(['car_id'=>$param['car_id'],'enclosure_id'=>$param['enclosure_id']])->find();

		if($is_exist) return showErrorMsg(L("_OPERATION_SUCCESS_"));

		$count = $model->where(['car_id'=>$param['car_id'],'enclosure_id'=>$param['enclosure_id']])->count();

		if ($count>8) return showErrorMsg('一个车最多只可以绑定8个围栏',100002);


		$insert_data = [
			'car_id'	=>	$param['car_id'],
			'enclosure_id'	=>	$param['enclosure_id'],
			'create_time'	=>	time()
		];

		$o = $model->add($insert_data);

		if($o) return showErrorMsg(L("_OPERATION_SUCCESS_"));
		return showErrorMsg(L("_OPERATION_FAIL_"),100002);
	}


    /**
     * @desc 围栏列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('enclosure');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,title,shape,lng,lat,radius,location_id,create_time,content',$listRows);
        if (!empty($list)){
            foreach ($list as &$val){
                $val['content']=unserialize($val['content']);
            }
        }

        return showList($list,$page);
    }

    /**
     * @desc 围栏详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $info = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

//        $info['content'] = !empty($info) ? unserialize($info['content']) : [];

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 添加围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,shape');

        unset($check);

        $model = M('enclosure');

        if (!empty($param['title'])) $insert_data['title'] = $param['title'];
        if (!empty($param['lat'])) $insert_data['lat'] = $param['lat'];
        if (!empty($param['lng'])) $insert_data['lng'] = $param['lng'];
        if (!empty($param['radius'])) $insert_data['radius'] = $param['radius'];
        if (!empty($param['shape'])) $insert_data['shape'] = $param['shape'];
        if (!empty($param['location_id'])) $insert_data['location_id'] = $param['location_id'];
        if (!empty($param['content'])) $insert_data['content'] = serialize($param['content']);

        $insert_data['create_time'] = time();
        $insert_data['uid'] = $this->userInfo->id;


        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 修改围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,title,shape');

        unset($check);

        $model = M('enclosure');

        if (!empty($param['title'])) $insert_data['title'] = $param['title'];
        if (!empty($param['lat'])) $insert_data['lat'] = $param['lat'];
        if (!empty($param['lng'])) $insert_data['lng'] = $param['lng'];
        if (!empty($param['radius'])) $insert_data['radius'] = $param['radius'];
        if (!empty($param['shape'])) $insert_data['shape'] = $param['shape'];
        if (!empty($param['location_id'])) $insert_data['location_id'] = $param['location_id'];
        if (!empty($param['content'])) $insert_data['content'] = serialize($param['content']);

        $insert_data['create_time'] = time();
        $insert_data['uid'] = $this->userInfo->id;


        $operation = $model->where(['id'=>$param['id']])->save($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 删除围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $ids_arr=explode(',',$param['id']);
        if (!empty($ids_arr)){
            foreach ($ids_arr as &$val){
                $info = M("enclosure")->where(['id'=>$val,'uid'=>$this->userInfo->id])->find();

                if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

                $operation = M("enclosure")->where(['id'=>$val,'uid'=>$this->userInfo->id])->delete();
            }
            return showErrorMsg(L("_OPERATION_SUCCESS_"));
        }
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 查看历史轨迹
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carHistoryLocation($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id,s_time,e_time');

        unset($check);

        $model = M('car_attr');

        $map['uid'] = $this->userInfo->id;
        $map['car_id'] = $param['car_id'];
        $map['create_time'] = array('between',array(strtotime($param['s_time']),strtotime($param['e_time'])));

        $list = $model->where($map)->field("car_id,uid,lng,lat,speed")->select();

        return showList($list,new \stdClass());
    }


    /**
     * @desc 发送cmd命令
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function sendCmd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type,uids,content');

        unset($check);


        $uidsArr = explode(',',$param['uids']);

        foreach ($uidsArr as $k=>$v){
            if (!empty($v)){
                $insert_data = [
                    'uid'   =>  $v,
                    'send_uid'  =>  $this->userInfo->id,
                    'title'     =>  empty($param['title'])?'':$param['title'],
                    'content'   =>  $param['content'],
                    'read_time' =>  '',
                    'is_read'   =>  '0',
                    'type'      =>  2,
                    'create_time'   =>  time(),
                    'warning_type'  =>  $param['type']
                ];
                $operation = M('message')->add($insert_data);
            }
        }
        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }


    //报警消息类别
    public function warningList($param){
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $data=[
            1=>[
                'type'=>1,
                'title'=>"出区域（围栏）报警"
            ],
            2=>[
                'type'=>2,
                'title'=>"入区域（围栏）报警"
            ],
            3=>[
                'type'=>3,
                'title'=>"电瓶拆除报警 （断电）"
            ],
            4=>[
                'type'=>4,
                'title'=>"温度超高报警"
            ],
            5=>[
                'type'=>5,
                'title'=>"温度超低报警"
            ],
            6=>[
                'type'=>6,
                'title'=>"湿度报警"
            ],
            7=>[
                'type'=>7,
                'title'=>"停车超时报警"
            ],
            8=>[
                'type'=>8,
                'title'=>"掉线报警"
            ],
            9=>[
                'type'=>9,
                'title'=>"上线报警"
            ],
            10=>[
                'type'=>10,
                'title'=>"电压低报警"
            ],
            11=>[
                'type'=>11,
                'title'=>"电压高报警"
            ],
            12=>[
                'type'=>12,
                'title'=>"开门报警"
            ],
            13=>[
                'type'=>13,
                'title'=>"制冷机开报警"
            ],
            14=>[
                'type'=>14,
                'title'=>"制冷机关报警"
            ],
            15=>[
                'type'=>15,
                'title'=>"油量低报警"
            ],
            16=>[
                'type'=>16,
                'title'=>"疲劳驾驶报警（行驶超过几个小时就报警）"
            ],
            17=>[
                'type'=>17,
                'title'=>"偏离线路报警（偏离围栏里的线型）"
            ],
            18=>[
                'type'=>18,
                'title'=>"GPS信号报警"
            ],
        ];

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$data);

    }

    public function carGroup($param){
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('uid');

        unset($check);

        $map['company_id']=$param['uid'];
        if (!empty($param['keyword'])){
            $map['title']=array('like','%'.$param['keyword'].'%');
        }

        $date=M('car_group')->where($map)->field("id,title,company_id")->order("num asc,id desc")->select();


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,empty($date)?array():$date);

    }

}